c:redirect is not working in weblogic server - spring

i have a spring application and while i am deploying it in weblogic server it is giving 404 Error.
in web.xml i given index.jsp as welcome file and from that i am redirecting to controller class but it is not working
here is my web.xml
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>*.do</url-pattern>
<url-pattern>/*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
here is my index.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<c:redirect url="/result"/>
</body>
</html>
here is my controller class method
#RequestMapping(value="/result" , method=RequestMethod.GET)
public String getResults(ModelMap map){
map.put("userList","null");
System.out.println("result page ...");
return "result";
}
the above logic is woking in tomcat but it is not working in weblogic server
please any body help me.
here i created as a war and i am deploying in weblogic server.

Can you change
<url-pattern>/*.html</url-pattern>
to
<url-pattern>/*.jsp</url-pattern>
Edit
Try doing the following
#RequestMapping(value="/result" , method=RequestMethod.GET)
public String getResults(ModelMap map){
map.put("userList","null");
System.out.println("result page ...");
return "redirect:"+ Your_Url;
}

Related

http status 500-Error instantiating servlet class org.springframework.web.servlet.DispatcherServlet

Above is the directory hierarchy of my program
I am new to spring and learning MVC concepts I have written a program which takes input(Name) into a text box and prints Hello...'name'. Tha following is my directory structure and the various files I have created.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>MVC_HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- default configuration -->
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.ap</url-pattern> <!-- this same extension should bbe used in form action -->
</servlet-mapping>
</web-app>
HelloWorld-servlet.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- default handler mapping -->
<!-- file should be created under web inf annd it's view resolver file -->
<!-- handler(Not rqd in case of default handler) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- controller configuration -->
<bean name="/HelloWorld.ap" class="controller.HelloController"> <!-- mapping url pattern to controller class using 'name' -->
<!-- view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" vlaue="/"/> <!-- default location (prefix used foor rqd page locations) -->
|<property name="sufix" value=".jsp"/> <!-- sufix used forr rqd page extensions -->
</bean>
</bean>
</beans>
HelloController.java
package controller;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.sun.javafx.collections.MappingChange.Map;
public class HelloController implements Controller {
#Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {
String name=req.getParameter("name");
Map m= new HashMap(); // creating output object
m.put("msg","Hello..."+name);
ModelAndView mav=new ModelAndView("success"+m);
return mav;
}
}
index.jsp
<h1> Hello World</h1>
<form action="./hello.ap">
NAME: <input type="text" name="name">
<input type="Submit" value="Say Hello">
</form>
success.jsp
${msg}
when I am running this code the index.jsp page is running properly bur upon further execution It shows Error 500. what's wrong with the code..?? I am using Eclipse oxygen in that apache 8.5
You configuration in web.xml are wrong.
You are trying to map the dispatch servlet as the controller.
In spring mvc like in other mvc frameworks (struts etc.) there is one major servlet that use to dispatch all requests.
org.springframework.web.servlet.DispatcherServlet is usually named “dispatcher” and should be mapped to a top level url usually “\”
e.g.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And the controller is mapped under this url e.g. HelloWorld
#Controller
#RequestMapping("/HelloWorld");"
public class HelloController implements Controller {}
As your initial project is far from the classic vanilla starter Spring MVC project and it looks like you are using a very old Spring version (or spring tutorial). I suggest to start fresh from some online tutorial.
E.g.
http://www.journaldev.com/2433/spring-mvc-tutorial
http://www.mkyong.com/spring-mvc/gradle-spring-mvc-web-project-example/
Try below edit to web.xml.
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>*.ap</url-pattern> <!-- this same extension should bbe used in form action -->
</servlet-mapping>

Request Mapping returning error 404

This is my controller that maps a request to this url http://localhost:8080/SpringMVCJSON/rest/kfc/brands
contoller file
#Controller
#RequestMapping("/kfc/brands")
public class JSONController {
#RequestMapping(value = "{name}", method = RequestMethod.GET)
public #ResponseBody
Shop getShopInJSON(#PathVariable String name) {
Shop shop = new Shop();
shop.setName(name);
shop.setStaffName(new String[] { "name1", "name2" });
return shop;
}
this is the web.xml with the servlet request that dispatches the request/response along with the url
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Assuming that everything is alright, when I launch my app on this url it returns error 404 http://localhost:8080/SpringMVCJSON/rest/kfc/brands
My server console returns this warning
Apr 26, 2016 12:14:47 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVCJSON/rest/kfc/brands] in DispatcherServlet with name 'mvc-dispatcher'
Please why is tomcat not mapping request to the server?
You configured your controller to be available on /kfc/brands/{name} URL but trying to access it on /kfc/brands.
Here you can find more information about using #RequestMapping: http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping

Unable to reach home.jsp

I have an app. Due to some issues, in my web.xml, I had to change the url pattern from "/" to "*.action", and added a welcome-file-list. This is my web.xml now:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
Before I did the change, I was able to reach home.jsp by simply typing localhost:8080/myapp in my browser. What should I type now to reach the home page?
Thank you.
NOTE: Im using Spring.
EDIT:
Forgot my HomeController:
#Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
#RequestMapping(value = "/")
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
Your request should ends with ".action" try to sent localhost:8080/myapp/some.action
And change mapping in controller to "/some.action"

Encoding problem using Spring MVC

I have a demo web application that creates users. When I try to insert data in other languages (like french) the characters are not encoded correctly. The code on the controller is:
#SuppressWarnings("unchecked")
#RequestMapping(value = "/user/create.htm", params={"id"}, method = RequestMethod.GET)
public String edit(#RequestParam("id") Long id, ModelMap model) {
System.out.println("id is " + id);
User user = userService.get(id);
model.put("user", user);
return "user/create";
}
#RequestMapping(value = "/user/create.htm", method = RequestMethod.POST)
public String save(#ModelAttribute("user") User user, BindingResult result) {
System.out.println(user.getFirstName());
System.out.println(user.getLastName());
validator.validate(user, result);
if(result.hasErrors()) {
return "user/create";
}
userService.save(user);
return "redirect:list.htm";
}
my web.xml is:
...
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
and the page is:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
...
<form:form method="post" commandName="user">
...
<form:input path="firstName" cssErrorClass="form-error-field"/>
...
when I enter some french characters in the first name then the output from the system.out.println is ????+????? or something similar.
I saw other people fixing this with the CharacterEncodingFilter but this doesn't seem to work.
Thanks a lot.
Edited the filter value.
Try making CharacterEncodingFilter the first filter in web.xml.
I realize this question is a little old, but I just ran into the same problem, and moving CharacterEncodingFilter fixed it for me.
If this still does not work and you're using Tomcat as your application server try to set the following option on every <Connector> element in the server.xml:
<Connector URIEncoding="UTF-8" ...>
...
</Connector>
This did the trick for me. There might be similar options for other application servers, so you might want to check the server documentation.
Perhaps I'm missing something, but if the page-encoding in your JSP is "UTF-8", shouldn't the encoding in your CharacterEncodingFilter be UTF-8 rather than ISO-8859-7?
You need to add accept-charset="UTF-8" to your form.
Output of System.out.println() depends on console encoding, so it's not a good way to debug encoding problems.
To check that your values are decoded properly, you should show it at another page. Actually, it's already done in the case of form validation failure, so your system works fine if values in the fields remains the same after validation error.
There are two things to help:
In your setenv.sh file add JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=utf-8"
In the script that starts your tomcat process, set the LANG environment: export LANG='utf-8'
This can then be tested by examining the default character set: Charset.defaultCharset().

Custom 404 using Spring DispatcherServlet

I've set up web.xml as below. I also have an annotation-based controller, which takes in any URL pattern and then goes to the corresponding jsp (I've set that up in the -servlet.xml). However, If I go to a page that ends in .html (and whose jsp doesn't exist), I don't see the custom 404 page (and see the below error in the log). Any page that doesn't end in .html, I can see the custom 404 page.
How can I configure to have a custom 404 page for any page that goes through the DispatcherServlet?
Also want to add that if I set my error page to a static page (ie. error.htm) it works, but if I change it to a jsp (ie. error.jsp), I get the IllegalStateException. Any help would be appreciated.
log error
Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:606)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
controller
#RequestMapping(value = {"/**"})
public ModelAndView test() {
ModelAndView modelAndView = new ModelAndView();
return modelAndView;
}
web.xml
<servlet>
<servlet-name>my_servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
...
<servlet-mapping>
<servlet-name>my_servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
...
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>
One option is to map all your error pages through your dispatcher servlet.
Create a new HTTP error controller:
#Controller
public class HTTPErrorController {
#RequestMapping(value="/errors/404.html")
public String handle404() {
return "errorPageTemplate";
}
#RequestMapping(value="/errors/403.html")
...
}
Map the error pages in web.xml
<error-page>
<error-code>404</error-code>
<location>/errors/404.html</location>
</error-page>

Resources