Solution of invalid Springmvc project jumping to controller
- 2021-11-10 09:45:28
- OfStack
Springmvc Jump controller Invalid
In the actual construction of Springmvc project, business processing and logical jump are carried out through controller. It is often found that the interface URL is spelled correctly but the control layer cannot be accessed. Here are two explanations for investigation:
1. springmvc-servlet. xml
(It only shows that other configurations of controller are added by themselves), and add them in the configuration file
<!-- Automatically scan packages to support annotations IOC -->
<context:component-scan base-package="com.bj58.automation.controller" /> // Path is added based on actual requirements
2. web. xml. In the configuration file,
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Modify to
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The controller format is as follows:
@Controller
@RequestMapping("/task")
public class TaskController {
@Autowired
InsertTask insertTask;// Injection adds task interface
@RequestMapping(value="/insertTask")
public void insertTask(@RequestParam("productId") int productId,@RequestParam("taskname") String taskname,
HttpServletRequest request,Model model) {
insertTask.insertTaskService(productId, taskname);
}
@ResponseBody
@RequestMapping(value="/demo",method=RequestMethod.GET)
public String name() {
System.err.println("demo123");
return "demo";
}
The interface access path format is as follows: http://localhost: 8080/task/demo
springmvc cannot enter controller, and no error is reported in the background
Talk about the solution
1. Check whether it is configured
Processor mapper, processor adapter
<mvc:annotation-driven />
2. Whether to configure
Scan controller packets
<context:component-scan base-package="cn.ssm.controller" />
3. Is controller class added
@ contaoller notes
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/item/{itemId}")
@ResponseBody
public TbItem getItemById(@PathVariable Long itemId) {
System.err.println(123);
TbItem tbItem = itemService.getItemById(itemId);
return tbItem;
}
}
4. View parser
Is it configured correctly
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
5. Is the web. xml file configured
springmvc file startup
<!-- springmvc Front-end controller of -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation It's not necessary, If you do not configure contextConfigLocation , springmvc The configuration file of defaults to: WEB-INF/servlet Adj. name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>